﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Workflow.Alerts.Sublayouts
{
    public partial class MaterialsGrid : BaseControl, IMaterialsGridView
    {
        private const string AlertMaterialId = "AlertMaterialId";

        private Int32? CurrentAlertMaterialId
        {
            get
            {
                if (Session[AlertMaterialId] == null)
                    return null;
                else
                    return Int32.Parse(Session[AlertMaterialId].ToString());
            }

            set
            {
                Session[AlertMaterialId] = value;
            }
        }

        #region IMaterialsGridView Members

        public int AlertId
        {
            get { return Int32.Parse(Alert); }
        }

        public List<MaterialGridClass> MaterialsTable
        {
            set
            {
                rptMaterialList.DataSource = value;
                rptMaterialList.DataBind();
            }
        }

        public System.Collections.Generic.Dictionary<int, string> MaterialsList
        {
            set
            {
                ddlMaterial.DataSource = value;
                ddlMaterial.DataTextField = "value";
                ddlMaterial.DataValueField = "key";
                ddlMaterial.DataBind();
            }
        }

        public int SelectedMaterial
        {
            get
            {
                return Int32.Parse(ddlMaterial.SelectedValue);
            }
            set
            {
                if (ddlMaterial.Items.FindByValue(value.ToString()) != null)
                    ddlMaterial.Items.FindByValue(value.ToString()).Selected = true;
            }
        }

        public int SelectedQuantity
        {
            get
            {
                return (int)txtQuantity.Value.Value;
            }
            set
            {
                txtQuantity.Value = value;
            }
        }

        public bool IsUitvoerder
        {
            get
            {
                return chkUitvoerder.Checked;
            }
            set
            {
                chkUitvoerder.Checked = value;
            }
        }

        public bool IsHerstel
        {
            get
            {
                return chkUitstel.Checked;
            }
            set
            {
                chkUitstel.Checked = value;
            }
        }

        public event OnMaterialsTableLoad OnTableLoad;

        public event OnEditFormLoad OnEditLoad;

        public event OnSaveMaterialAlert OnSave;

        public event OnDeleteMaterialAlert OnDelete;

        public event OnGridFirstLoad OnFirstLoad;

        public event OnGridClearTempValues OnClearTempValues;

        public Boolean StoreChangesInDb
        {
            get
            {
                return SaveChangesInDB;
            }
        }

        #endregion

        #region Public properties
        public String Alert { get; set; }

        public Boolean ShowHerstelCheckbox
        {
            set
            {
                DivHerstel.Visible = value;
            }

        }

        public Boolean SaveChangesInDB { get; set; }

        public Boolean IsEditable { get; set; }

        public Boolean ShowMaterialValidator
        {
            set
            {
                ltMaterialsValidation.Visible = value;
            }
        }

        public void ClearTempValues()
        {
            if (OnClearTempValues != null)
                OnClearTempValues();
        }
        #endregion

        #region Private Methods

        private void ShowListDiv()
        {
            DivMaterialEdit.Visible = false;
            DivMaterialsList.Visible = true;

            CurrentAlertMaterialId = null;
        }

        private void ShowEditDiv()
        {
            DivMaterialEdit.Visible = true;
            DivMaterialsList.Visible = false;
        }
        #endregion

        #region Event Handlers
        protected void Page_Load(object sender, EventArgs e)
        {
            IsEditable = Sitecore.Context.Item.Parent.Name != WorkflowConstants.Logged;

            if (string.IsNullOrEmpty(Alert))
                return;
            PresentationManager.RegisterView(typeof(MaterialsGridViewPresenter), this, new WebSessionProvider());
            if (!IsPostBack)
            {
                if (!StoreChangesInDb && OnFirstLoad != null)
                {
                    OnFirstLoad();
                }

                if (OnTableLoad != null)
                {
                    OnTableLoad();
                }
            }

            if (!IsEditable)
            {
                btnNewMaterial.Visible = false;
            }
        }

        protected void btnNewMaterial_Click(object sender, EventArgs e)
        {
            if (OnEditLoad != null)
            {
                OnEditLoad(null);
                ShowEditDiv();
            }
        }

        protected void rptMaterialList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                if (OnEditLoad != null)
                {
                    int alertMaterialId = Int32.Parse(e.CommandArgument.ToString());
                    OnEditLoad(alertMaterialId);
                    CurrentAlertMaterialId = alertMaterialId;
                    ShowEditDiv();
                }
            }
            else if (e.CommandName == "Delete")
            {
                if (OnDelete != null)
                {
                    OnDelete(Int32.Parse(e.CommandArgument.ToString()));
                    if (PriceUpdated != null)
                        PriceUpdated(source, null);
                }
            }
        }

        protected void rptMaterialList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                MaterialGridClass mgc = (MaterialGridClass)e.Item.DataItem;
                if (mgc != null)
                {
                    var imgUitvoerder = (Image)e.Item.FindControlRecursive("imgUitvoerder");
                    if (imgUitvoerder != null)
                    {
                        imgUitvoerder.CssClass = mgc.uitvoerder ? "selected" : "noselected";
                        imgUitvoerder.ImageUrl = mgc.uitvoerder ? "/images/list_statusIcon_approve.png" : "/images/list_statusIcon_empty.png";
                    }

                    var imgUitstel = (Image)e.Item.FindControlRecursive("imgUitstel");
                    if (imgUitstel != null)
                    {
                        imgUitstel.CssClass = mgc.uitstel ? "selected" : "noselected";
                        imgUitstel.ImageUrl = mgc.uitstel ? "/images/list_statusIcon_approve.png" : "/images/list_statusIcon_empty.png";
                    }

                    var btnEdit = (Button)e.Item.FindControlRecursive("btnEdit");
                    if (btnEdit != null)
                    {
                        btnEdit.CommandArgument = mgc.id.ToString();
                        btnEdit.Visible = IsEditable;
                    }

                    var btnDelete = (Button)e.Item.FindControlRecursive("btnDelete");
                    if (btnDelete != null)
                    {
                        btnDelete.CommandArgument = mgc.id.ToString();
                        btnDelete.Visible = IsEditable;
                    }

                }
            }
        }

        protected void btnSaveNewMaterial_Click(object sender, EventArgs e)
        {
            if (OnSave != null)
            {
                OnSave(CurrentAlertMaterialId);
                ShowListDiv();
                if (PriceUpdated != null)
                    PriceUpdated(sender, null);
            }
        }

        protected void btnCancelNewMaterial_Click(object sender, EventArgs e)
        {
            ShowListDiv();
        }
        #endregion

        #region Events

        public delegate void MaterialsUpdatePrice(object sender, EventArgs e);

        public event MaterialsUpdatePrice PriceUpdated;

        #endregion

    }
}